package test2.service.impl;

import test2.entity.Test2;
import com.perye.dokit.utils.ValidationUtil;
import com.perye.dokit.utils.FileUtil;
import test2.repository.Test2Repository;
import test2.service.Test2Service;
import test2.dto.Test2Dto;
import test2.query.Test2QueryCriteria;
import test2.mapper.Test2Mapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.perye.dokit.utils.PageUtil;
import com.perye.dokit.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

@Service
//@CacheConfig(cacheNames = "test2")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class Test2ServiceImpl implements Test2Service {

    private final Test2Repository test2Repository;

    private final Test2Mapper test2Mapper;

    public Test2ServiceImpl(Test2Repository test2Repository, Test2Mapper test2Mapper) {
        this.test2Repository = test2Repository;
        this.test2Mapper = test2Mapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(Test2QueryCriteria criteria, Pageable pageable){
        Page<Test2> page = test2Repository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(test2Mapper::toDto));
    }

    @Override
    //@Cacheable
    public List<Test2Dto> queryAll(Test2QueryCriteria criteria){
        return test2Mapper.toDto(test2Repository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public Test2Dto findById(Long id) {
        Test2 test2 = test2Repository.findById(id).orElseGet(Test2::new);
        ValidationUtil.isNull(test2.getId(),"Test2","id",id);
        return test2Mapper.toDto(test2);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public Test2Dto create(Test2 resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setId(snowflake.nextId());
        return test2Mapper.toDto(test2Repository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(Test2 resources) {
        Test2 test2 = test2Repository.findById(resources.getId()).orElseGet(Test2::new);
        ValidationUtil.isNull( test2.getId(),"Test2","id",resources.getId());
        test2.copy(resources);
        test2Repository.save(test2);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            test2Repository.deleteById(id);
        }
    }

    @Override
    public void download(List<Test2Dto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (Test2Dto test2 : all) {
            Map<String,Object> map = new LinkedHashMap<>();
                         map.put(" name",  test2.getName());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

 }
